package com.eshop.service.Impl;

import com.eshop.dao.OrderInfoDao;
import com.eshop.pojo.OrderDetail;
import com.eshop.pojo.OrderInfo;
import com.eshop.pojo.Pager;
import com.eshop.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("OrderInfoService")
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public class OrderInfoServiceImpl implements OrderInfoService {
    @Autowired
    private OrderInfoDao orderInfoDao;

    @Override
    public List<OrderInfo> selectByPage(OrderInfo orderInfo, Pager pager) {
        Map<String,Object> param = new HashMap<>();
        param.put("orderInfo",orderInfo);
        int resultCount = orderInfoDao.count(param);
        if (resultCount > 0){
            pager.setRowCount(resultCount);
            param.put("pager",pager);
        }
        return orderInfoDao.selectByPage(param);
    }

    @Override
    public int count(Map<String, Object> param) {
        return orderInfoDao.count(param);
    }

    @Override
    public int saveOderInfo(OrderInfo orderInfo) {
        return orderInfoDao.saveOderInfo(orderInfo);
    }

    @Override
    public int saveOrderDetail(OrderDetail detail) {
        return orderInfoDao.saveOrderDetail(detail);
    }

    @Override
    public OrderInfo selectOrderInfoById(int id) {
        return orderInfoDao.selectOrderInfoById(id);
    }

    @Override
    public List<OrderDetail> selectOrderDetailByOid(int oid) {
        return orderInfoDao.selectOrderDetailByOid(oid);
    }

    @Override
    public int deleteOrder(int id) {
        int i;
        try {
            orderInfoDao.deleteOrderDetail(id);
            i = orderInfoDao.deleteOrderInfo(id);
        } catch (Exception e) {
            e.printStackTrace();
            i = 0;
        }
        return i;
    }
}
